import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Clipboard } from '@ionic-native/clipboard';
import { Screenshot } from '@ionic-native/screenshot';
import { PhotoLibrary } from '@ionic-native/photo-library';
import { UnitsProvider } from '../../providers/units/units';
/*
  Generated class for the ShareProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
declare let Wechat;  // 此处声明plugin.xml中clobbers对应的值
@Injectable()
export class ShareProvider {

  public SHARE_HTTP_URL = 'http://m.xuanwo123.com';

  constructor(
    public units: UnitsProvider,
    public clipboard: Clipboard,
    public screenshot: Screenshot,
    public photoLibrary: PhotoLibrary) {
    console.log('Hello ShareProvider Provider');
  }

  //执行分享操作
  public doShare(obj: any) {
    //微信app分享
    if (this.units.isApp()) {
      return this.WechatAppShare(obj);
    }
  }

  //复制路径
  public doCopyUrl(obj: any) {
    return new Promise((success, fail) => {
      //复制链接
      this.clipboard.copy(this.SHARE_HTTP_URL).then(function (data) {
        if (success)
          success();
      }, function (error) {
        if (fail)
          fail();
      });
    });
  }

  //保存屏幕截屏
  public doScreenShot() {
    //保存截图
    let that = this;
    return new Promise((success, fail) => {
      this.screenshot.URI(80).then(function (data) {
        //请求权限
        that.photoLibrary.requestAuthorization().then(() => {
          //保存图片
          that.photoLibrary.saveImage(data['URI'], "images", {}).then((res) => {
            if (success)
              success();
          });
        });
      }, function (error) {
        if (fail)
          fail();
      });
    });
  }

  //微信app分享
  private WechatAppShare(obj: any) {
    let that = this;
    return new Promise((success, fail) => {
      //分享数据
      let payParams = {
        message: {
          title: that.getShareTitle(obj),
          description: that.getShareDesc(obj),
          thumb: that.getShareThumb(obj),
          media: {
            type: Wechat.Type.WEBPAGE,
            webpageUrl: that.getShareLink(obj)
          }
        },
        scene: Wechat.Scene.SESSION // 分享好友
      };
      //微信分享
      Wechat.share(payParams, (result) => {
        if (success)
          success();
      }, (error) => {
        if (fail)
          fail();
      });
    });
  }

  //标题
  private getShareTitle(params: any) {
    if (params.title != undefined) {
      return params.title;
    } else {
      return "炫窝";
    }
  }
  //内容
  private getShareDesc(params: any) {
    if (params.desc != undefined) {
      return params.desc;
    } else {
      return "炫窝，卓越的年华，追求更高性价比！";
    }
  }
  //图片
  private getShareThumb(params: any) {
    if (params.thumb != undefined) {
      return params.thumb;
    } else {
      return this.SHARE_HTTP_URL + "/logo.png";
    }
  }
  //链接内容
  private getShareLink(params: any) {
    if (params.link != undefined) {
      return this.SHARE_HTTP_URL + "/index.html?" + params.link;
    } else {
      return this.SHARE_HTTP_URL + "/index.html";
    }
  }
}
